package com.lxh.lxhcrm.model.service;

import com.lxh.lxhcrm.model.dao.productManagement.*;
import com.lxh.lxhcrm.model.mappers.productManagement.ProductMapper;
import com.lxh.lxhcrm.model.pojos.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class ProductService {
    @Resource
    ProductMapper productMapper;
    /*
     * 查询
     * */
    //产品列表查询
    public List<ProDetail> findAllProduct(String searchText,Integer selectModel,Integer proClassilyId){
        return productMapper.findAllProduct(searchText,selectModel,proClassilyId);
    }
    //无分页查询产品
    public List<ProDetail> allProduct(){
        return productMapper.allProduct();
    }
    //查询产品属性
    public List<Attribute> findAllAttribute(){return productMapper.findAllAttribute();}
    //产品分类查询，树形结构
    public List<ProClassily> findAllClassily(){ return productMapper.findAllClassily();}
    //产品分类列表查询
    public List<ProClassily> findClassily(){return productMapper.findClassily();}
    //产品分类查询
    public List<Classily> onlyClassily(){return productMapper.onlyClassily();}
    //查询合同中销售产品数量数据
    public List<Map<String,Object>> findCPRoNum(Integer yearTime, Integer monthTime){
        return productMapper.findCPRoNum(yearTime,monthTime);
    }
    //查询采购中产品数量数据
    public List<Map<String,Object>> findCaiProNum(Integer yearTime, Integer monthTime){
        return productMapper.findCaiProNum(yearTime,monthTime);
    }
    /*
     * 新增
     * */
    @Resource
    ProClassilyDao proClassilyDao;
    //新增产品分类
    public void addClassily(ProClassily classily){
        proClassilyDao.save(classily);
    }
    @Resource
    AttributeDao attributeDao;
    @Resource
    AbilityDao abilityDao;
    //新增产品属性
    public void addAttribute(Attribute attribute){
        attributeDao.save(attribute);
    }
    public void addAbility(Ability ability){
        abilityDao.save(ability);
    }
    @Resource
    ProductDao productDao;
    @Resource
    ProDetailDao proDetailDao;
    //产品新增
    public void addProduct(Product product){
        Product pro=productDao.save(product);
        for(ProDetail proDetail:product.getProDetails()){
            proDetail.setProduct(pro);
            ProDetail detail=addProDetail(proDetail);
            for(AbiCen abiCen:proDetail.getAbiCens()){
                abiCen.setProDetail(detail);
                addAbiCen(abiCen);
            }
        }
    }
    //产品详情新增
    public ProDetail addProDetail(ProDetail proDetail){
        ProDetail detail= proDetailDao.save(proDetail);
        return detail;
    }
    @Resource
    AbiCenDao abiCenDao;
    //为产品添加属性
    public void addAbiCen(AbiCen abiCen){abiCenDao.save(abiCen);}
    /*
     * 修改
     * */
    //修改产品状态，一键下架所选产品
    public void changeProOn(Integer proDetailId){productMapper.changeProOn(proDetailId);}
}
